`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company: 
// Engineer: 
// 
// Create Date: 2025/01/27 10:39:22
// Design Name: 
// Module Name: time_ctrl
// Project Name: 
// Target Devices: 
// Tool Versions: 
// Description: 
// 
// Dependencies: 
// 
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
// 
//////////////////////////////////////////////////////////////////////////////////


module time_cal #(parameter MAX_1s = 12_000_000)
	(
	input 				clk				,
	input 				rst             ,
	input				key2_pulse      ,
	input		[3:0]	oled_mode       ,
	output reg 	[7:0]	curr_hour1      ,
	output reg 	[7:0]	curr_hour0      ,
	output reg 	[7:0]	curr_min1       ,
	output reg 	[7:0]	curr_min0       ,
	output reg 	[7:0]	curr_sec1       ,
	output reg 	[7:0]	curr_sec0       ,
	output reg 	[7:0]	next_hour1      ,
	output reg 	[7:0]	next_hour0      ,
	output reg 	[7:0]	next_min1       ,
	output reg 	[7:0]	next_min0       ,
	output reg 	[7:0]	next_sec1       ,
	output reg 	[7:0]	next_sec0		,
	output  	[7:0]	diff_hour1      ,
	output  	[7:0]	diff_hour0      ,
	output  	[7:0]	diff_min1       ,
	output  	[7:0]	diff_min0       ,
	output  	[7:0]	diff_sec1       ,
	output  	[7:0]	diff_sec0		,
	output 	  			beeper_start	,
	output 				beeper_stop		,
	output 				beeper_switch	,
	output 				beeper_en		,
    output reg [5:0] 	hour      			// 鐏忓繑妞�
    );
	
    // 鐠佲剝鏆熼崳锟�
    reg [23:0] counter;  // 閻劍娼电拋鈩冩殶MAX_1s閸涖劍婀�
    reg [5:0] sec;       // 缁夛拷
    reg [5:0] min;       // 閸掑棝鎸�
    // reg [5:0] hour;      // 鐏忓繑妞�
	
    // 閸愬懘鍎撮弮鍫曟？鐎靛嫬鐡ㄩ崳锟�
    reg [5:0] next_sec;       // 缁夛拷
    reg [5:0] next_min;       // 閸掑棝鎸�
    reg [5:0] next_hour;      // 鐏忓繑妞�
	
	
    wire  [5:0] 	diff_hour		;    	// 鐠侊紕鐣婚弮鍫曟？瀹割噯绱扮亸蹇旀
    wire  [5:0] 	diff_min 		;   	// 鐠侊紕鐣婚弮鍫曟？瀹割噯绱伴崚鍡涙寭
    wire  [5:0] 	diff_sec 		; 		// 鐠侊紕鐣婚弮鍫曟？瀹割噯绱扮粔锟� 
	

	
	
	//娑撳秴褰堥幐澶愭暛瑜板崬鎼烽惃鍕纯閹恒儲鏆熺€涙鎸撶拋鈩冩殶
	// always @(posedge clk or negedge rst) begin
		// if (!rst) begin
			// counter <= 0; sec <= 27; min <= 54; hour <= 1;
			// curr_hour0 <= 0; curr_hour1 <= 0; curr_min0 <= 0; curr_min1 <= 0;
			// curr_sec0 <= 0; curr_sec1 <= 0;
		// end else begin
			// if (counter == MAX_1s - 1) begin counter <= 0; 
				// if (sec == 59) begin sec <= 0; 
					// if (min == 59) begin min <= 0; 
						// hour <= (hour == 11) ? 0 : hour + 1; // 12鐏忓繑妞傞崚锟�
					// end else min <= min + 1; // 閸掑棝鎸�+1
				// end else sec <= sec + 1; // 缁夛拷+1
			// end else counter <= counter + 1;
			
			// // 閺嶇厧绱￠崠鏍ㄦ闂傛潙鍩岄弰鍓с仛閻ㄥ嫬鐦庣€涙ê娅�
			// curr_hour1 <= hour / 10; curr_hour0 <= hour % 10;
			// curr_min1 <= min / 10; curr_min0 <= min % 10;
			// curr_sec1 <= sec / 10; curr_sec0 <= sec % 10;
		// end
	// end
	
	//閺佹澘鐡ч柦鐔诲殰婢х偘绗岄幐澶愭暛閸欑姴濮�
	always @(posedge clk or negedge rst) begin
		if (!rst) begin
			counter <= 0; sec <= 20; min <= 33; hour <= 2;
			curr_hour0 <= 0; curr_hour1 <= 0; curr_min0 <= 0; curr_min1 <= 0;
			curr_sec0 <= 0; curr_sec1 <= 0;
		end else begin
			if (counter == MAX_1s - 1) begin counter <= 0; 
				if(key2_pulse)begin
					case(oled_mode)
						3:begin//閹稿鏁拫鍐Ν缁夛拷
							if (sec == 58) begin sec <= 0; 
								if (min == 59) begin min <= 0; 
									hour <= (hour == 11) ? 0 : hour + 1; // 12鐏忓繑妞傞崚锟�
								end else min <= min + 1; // 閸掑棝鎸�+1
							end else sec <= sec + 2; // 缁夛拷+1
						end
						2:begin//閹稿鏁拫鍐Ν閸掞拷
							if (sec == 59) begin sec <= 0; 
								if (min == 58) begin min <= 0; 
									hour <= (hour == 11) ? 0 : hour + 1; // 12鐏忓繑妞傞崚锟�
								end else min <= min + 1; // 閸掑棝鎸�+1
							end else begin
								sec <= sec + 1; // 缁夛拷+1
								min <= min + 1;
							end
						end
						1:begin//閹稿鏁拫鍐Ν鐏忓繑妞�
							if (sec == 59) begin sec <= 0; 
								if (min == 59) begin min <= 0; 
									hour <= (hour == 10) ? 0 : hour + 1; // 12鐏忓繑妞傞崚锟�
								end else min <= min + 1; // 閸掑棝鎸�+1
							end else begin 
								sec <= sec + 1; // 缁夛拷+1
								hour <= hour + 1;
							end
						end
						default:begin //閺佹澘鐡ч柦鐔活吀閺冭泛鍩岄弫瀵割潡閻ㄥ嫬鎮撻弮鏈电瘍濡偓濞村鍩岄幐澶愭暛閿涘奔绲鹃弰顖濐嚉閹稿鏁崗鏈电铂濡€崇础娑撳秳绱扮€佃鏆熺€涙鎸撴禍褏鏁撹ぐ鍗炴惙
							if (sec == 59) begin sec <= 0; 
								if (min == 59) begin min <= 0; 
									hour <= (hour == 11) ? 0 : hour + 1; // 12鐏忓繑妞傞崚锟�
								end else min <= min + 1; // 閸掑棝鎸�+1
							end else sec <= sec + 1; // 缁夛拷+1
						end
					endcase
				end else begin //閺佹澘鐡ч柦鐔活吀閺冭泛鍩岄弫瀵割潡娴ｅ棙妲稿▽鈩冩箒濡偓濞村鍩岄幐澶愭暛
					if (sec == 59) begin sec <= 0; 
						if (min == 59) begin min <= 0; 
							hour <= (hour == 11) ? 0 : hour + 1; // 12鐏忓繑妞傞崚锟�
						end else min <= min + 1; // 閸掑棝鎸�+1
					end else sec <= sec + 1; // 缁夛拷+1
				end
				
			//////////////////////////////////////////////////////////////////////////////
			//閺佹澘鐡ч柦鐔哥梾閺堝顓搁弮璺哄煂閺佸顫�
			end else begin counter <= counter + 1;
				if (key2_pulse == 1) begin//閺佹澘鐡ч柦鐔哥梾閺堝顓搁弮璺哄煂閺佸顫�,娴ｅ棙妲稿Λ鈧ù瀣煂娴滃棙瀵滈柨锟�
					case (oled_mode)
						3: begin // oled_mode == 3閿涘苯顤冮崝锟�1缁夛拷
							if (sec == 59) begin sec <= 0; 
								if (min == 59) begin min <= 0; 
									hour <= (hour == 11) ? 0 : hour + 1; // 12鐏忓繑妞傞崚锟�
								end else min <= min + 1; // 閸掑棝鎸�+1
							end else sec <= sec + 1; // 缁夛拷+1
						end
						2: begin // oled_mode == 2閿涘苯顤冮崝锟�1閸掑棝鎸�
							if (min == 59) begin min <= 0; 
								hour <= (hour == 11) ? 0 : hour + 1; // 12鐏忓繑妞傞崚锟�
							end else min <= min + 1; // 閸掑棝鎸�+1
						end
						1: begin // oled_mode == 1閿涘苯顤冮崝锟�1鐏忓繑妞�
							hour <= (hour == 11) ? 0 : hour + 1; // 12鐏忓繑妞傞崚锟�
						end

						default: begin
							// 姒涙顓婚幆鍛枌娑撳绱濇穱婵囧瘮閺冨爼妫挎稉宥呭綁
						end
					endcase
				end
			end
			
			// 閺嶇厧绱￠崠鏍ㄦ闂傛潙鍩岄弰鍓с仛閻ㄥ嫬鐦庣€涙ê娅�
			curr_hour1 <= hour / 10; curr_hour0 <= hour % 10;
			curr_min1 <= min / 10; curr_min0 <= min % 10;
			curr_sec1 <= sec / 10; curr_sec0 <= sec % 10;
		end
	end
	
	
	
	
	
	
	
// 濮ｅ粰AX_1s娑擃亜鎳嗛張鐔惰礋1缁夋帪绱濋悽銊ょ艾閺囧瓨鏌奵urr_閺冨爼妫�
    always @(posedge clk or negedge rst) begin
        if (!rst) begin
            next_sec <= 33;next_min <= 33;next_hour <= 2;
            next_hour0 <= 0;next_hour1 <= 0;next_min0 <= 0;next_min1 <= 0;
            next_sec0 <= 0;next_sec1 <= 0;
        end else begin // 閹貉冨煑next_閺冨爼妫�
            if (key2_pulse == 1) begin
                case (oled_mode)
                    6: begin // oled_mode == 6閿涘苯顤冮崝锟�1缁夛拷
						if (next_sec == 59) begin next_sec <= 0; 
							if (next_min == 59) begin next_min <= 0; 
								next_hour <= (next_hour == 11) ? 0 : next_hour + 1; // 12鐏忓繑妞傞崚锟�
							end else next_min <= next_min + 1; // 閸掑棝鎸�+1
						end else next_sec <= next_sec + 1; // 缁夛拷+1
                    end
                    5: begin // oled_mode == 5閿涘苯顤冮崝锟�1閸掑棝鎸�
						if (next_min == 59) begin next_min <= 0; 
							next_hour <= (next_hour == 11) ? 0 : next_hour + 1; // 12鐏忓繑妞傞崚锟�
						end else next_min <= next_min + 1; // 閸掑棝鎸�+1
                    end
                    4: begin // oled_mode == 4閿涘苯顤冮崝锟�1鐏忓繑妞�
						next_hour <= (next_hour == 11) ? 0 : next_hour + 1; // 12鐏忓繑妞傞崚锟�
                    end

                    default: begin
                        // 姒涙顓婚幆鍛枌娑撳绱濇穱婵囧瘮閺冨爼妫挎稉宥呭綁
                    end
                endcase
            end

            // 閺嶇厧绱￠崠鏉梕xt閺冨爼妫块崚鐗堟▔缁€铏规畱鐎靛嫬鐡ㄩ崳锟�
            next_hour1 <= next_hour / 10;next_hour0 <= next_hour % 10;
            next_min1 <= next_min / 10;next_min0 <= next_min % 10;
            next_sec1 <= next_sec / 10;next_sec0 <= next_sec % 10;
        end
    end
	
	
	
	disparity_time u1_disparity_time(
	  .clk						(clk			),
	  .rst                      (rst          	),
      .sec                      (sec          	),
      .min                      (min          	),
      .hour                     (hour         	),
      .next_sec                 (next_sec     	),
      .next_min                 (next_min     	),
      .next_hour	            (next_hour		),
	  .diff_hour1               (diff_hour1   	),
	  .diff_hour0               (diff_hour0   	),
	  .diff_min1                (diff_min1    	),
	  .diff_min0                (diff_min0    	),
	  .diff_sec1                (diff_sec1    	),
	  .diff_sec0                (diff_sec0    	),
	  .diff_hour				(diff_hour		),    	// 鐠侊紕鐣婚弮鍫曟？瀹割噯绱扮亸蹇旀
      .diff_min 				(diff_min 		),    	// 鐠侊紕鐣婚弮鍫曟？瀹割噯绱伴崚鍡涙寭
      .diff_sec 				(diff_sec 		)   	// 鐠侊紕鐣婚弮鍫曟？瀹割噯绱扮粔锟� 
    );
	
	
	beeper_ctrl u2_beeper_ctrl(
	  .clk						(clk			),
	  .rst			            (rst			),
	  .diff_hour	            (diff_hour	    ),
	  .diff_min 	            (diff_min 	    ),
	  .diff_sec 	 	        (diff_sec 	 	),
	  .key2_pulse               (key2_pulse     ),
	  .oled_mode                (oled_mode      ),
	  .beeper_start	            (beeper_start	),
	  .beeper_stop	            (beeper_stop	),
	  .beeper_switch            (beeper_switch  ),
	  .beeper_en				(beeper_en		)
	);
	
endmodule
